package mamma.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import mamma.common.FileService;
import mamma.common.PageNavigator;
import mamma.dao.jinkyuDao;
import mamma.dao.sanghyunDao;
import mamma.vo.MemberVo;
import mamma.vo.PhotoVo;

import com.opensymphony.xwork2.ActionSupport;

public class sanghyunAction extends ActionSupport implements ServletRequestAware, SessionAware{
   private int id;
   private File upload;
   private String uploadFileName;
   private String uploadContentType;
   private PhotoVo photo;
   private HttpServletRequest request;
   private Map<String, Object> session;
   private MemberVo memberVo;
   private String savedFile;
   private List<PhotoVo> photoList;
   private int currentPage = 1;
   private PageNavigator navi;
   
   
   
   /*사진을 넣고 프로필 바꾸기*/
   public String insertProfile() throws Exception{
      //프로필로 등록한 사진 서버에 업로드
      photo = new PhotoVo();
      FileService fs = new FileService();
      String basePath = ServletActionContext.getServletContext().getRealPath("/photoData");
            
      String savedfile = fs.saveFile(upload, basePath, uploadFileName);
      photo.setSavedFile(savedfile);
      photo.setOriginalFile(uploadFileName);
      

      // session 에서 회원정보 가져와서 방금 업로드한 프로필 사진 setting 하기
      MemberVo loginVo =(MemberVo)session.get("login");
      
      String email = loginVo.getEmail();
      String nickname = loginVo.getNickname();
      photo.setEmail(email);
      photo.setNickname(nickname);
      
      sanghyunDao dao = new sanghyunDao();
      dao.insertProfile(photo);
      int profile_id = dao.selectId(photo.getSavedFile());
      System.out.println("profile_id"+profile_id);
      dao.memberUpdate(email, profile_id);
      return SUCCESS;
   }
   
   /*사진 넣기*/
   public String insertPhoto() throws Exception{
      FileService fs = new FileService();
      
      /* memberVo에 정보 넣기 */
      memberVo = (MemberVo)session.get("login");
      
      sanghyunDao dao= new sanghyunDao();
      
      
      String basePath = ServletActionContext.getServletContext().getRealPath("/photoData");
      String savedfile = fs.saveFile(upload, basePath, uploadFileName);
      /* photo update에 필요한 정보 넣기*/
      photo.setSavedFile(savedfile);
      photo.setOriginalFile(uploadFileName);
      photo.setEmail(memberVo.getEmail());
      photo.setNickname(memberVo.getNickname());
      dao.insertPhoto(photo);
      return SUCCESS;
   }
   /*session의 email로 member 객체를 가져 옴*/
   public String getProfileInfo(){
      memberVo = new MemberVo();
      MemberVo temp = (MemberVo)session.get("login");
      String email = temp.getEmail();
      sanghyunDao dao= new sanghyunDao();
      
      memberVo = dao.getMember(email);
      savedFile = dao.getProfile(memberVo.getProfile_id());
      
      return SUCCESS;
   }
   /* 별명 비밀번호 거주지역 변 to the 경 */
   public String profileUpdate(){
      
      sanghyunDao dao = new sanghyunDao();
      dao.updateProfile(memberVo);
      
      return SUCCESS;
   }
   /* 사진 객체 찾아 savedFile 와 ID 값 가져오기*/
   public String photoList(){
      /*int countPerPage = Integer.parseInt(getText("board.countperpage"));      //페이지당 글목록 수
      int pagePerGroup = Integer.parseInt(getText("board.pagepergroup"));      //그룹당 페이지 수
      */
         
      System.out.println("currentPage : "+ currentPage);
      
      memberVo = new MemberVo();
      if (session.get("login")==null) {
      }
      MemberVo temp = (MemberVo)session.get("login");
      String email = temp.getEmail();
      
      /*user.properties값을 못읽어와서 직접 셋팅 */
      int countPerPage = 5;
      int pagePerGroup= 5;
      
      sanghyunDao dao = new sanghyunDao();
      int totalRecordsCount = dao.totalPhoto(email);
      
      System.out.println("totalRecordsCount: "+totalRecordsCount);
      PageNavigator navi = new PageNavigator(countPerPage, pagePerGroup, currentPage, totalRecordsCount);
      
      System.out.println("sdsd"+navi.getStartRecord()+ navi.getCountPerPage());
      
      photoList = dao.photoList(email, navi.getStartRecord(), navi.getCountPerPage());
      
      System.out.println(photoList);
      
      return SUCCESS;
   }
   
   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public File getUpload() {
      return upload;
   }

   public void setUpload(File upload) {
      this.upload = upload;
   }

   public String getUploadFileName() {
      return uploadFileName;
   }

   public void setUploadFileName(String uploadFileName) {
      this.uploadFileName = uploadFileName;
   }

   public String getUploadContentType() {
      return uploadContentType;
   }

   public void setUploadContentType(String uploadContentType) {
      this.uploadContentType = uploadContentType;
   }

   public PhotoVo getPhoto() {
      return photo;
   }

   public void setPhoto(PhotoVo photo) {
      this.photo = photo;
   }

   public HttpServletRequest getRequest() {
      return request;
   }

   public void setRequest(HttpServletRequest request) {
      this.request = request;
   }

   public MemberVo getMemberVo() {
      return memberVo;
   }

   public void setMemberVo(MemberVo memberVo) {
      this.memberVo = memberVo;
   }

   public Map<String, Object> getSession() {
      return session;
   }
   
   public String getSavedFile() {
      return savedFile;
   }

   public void setSavedFile(String savedFile) {
      this.savedFile = savedFile;
   }
   
   public List<PhotoVo> getPhotoList() {
      return photoList;
   }

   public void setPhotoList(List<PhotoVo> photoList) {
      this.photoList = photoList;
   }

   @Override
   public void setServletRequest(HttpServletRequest request) {
      this.request = request;
      
   }

   @Override
   public void setSession(Map<String, Object> session) {
      this.session = session;
   }

   public int getCurrentPage() {
      return currentPage;
   }

   public void setCurrentPage(int currentPage) {
      this.currentPage = currentPage;
   }

   public PageNavigator getNavi() {
      return navi;
   }

   public void setNavi(PageNavigator navi) {
      this.navi = navi;
   }
   
}